<template>
	<view>
		<!-- 安全设置-修改密码 -->
		<!-- #ifdef APP-PLUS -->
		<nav-bar title="安全设置-修改密码" leftIcon="back" @click-left="back">
			<block slot="right"><button class="cu-btn bg-blue margin-right-sm" @tap="save" :disabled="btndisabled">保存</button></block>
		</nav-bar>
		<!-- #endif -->
		<view class="cu-form-group padding-lr">
			<view class="title" style="width: 150rpx;">原始密码</view>
			<input type="text" v-model="oldPwd" password placeholder="请输入原始密码" placeholder-class="text-gray text-df" />
		</view>
		<view class="cu-form-group padding-lr">
			<view class="title" style="width: 150rpx;">新密码</view>
			<input type="text" v-model="newPwd" password placeholder="请输入新密码" placeholder-class="text-gray text-df" />
		</view>
		<view class="cu-form-group padding-lr">
			<view class="title" style="width: 150rpx;">确认密码</view>
			<input type="text" v-model="newPwdvlt" password placeholder="请输入确认密码" placeholder-class="text-gray text-df" />
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<userButton @submitEvent="save" :btnDisabled="btndisabled">保存</userButton>
		<!-- #endif -->
		<loading :loadModal="loadModal"></loading>
	</view>
</template>

<script>
import userButton from '@/components/user-button.vue';
var _self; //this容器
export default {
	components: {
		userButton
	},
	data() {
		return {
			oldPwd: '',
			newPwd: '',
			newPwdvlt: '',
			loadModal: false
		};
	},
	computed: {
		btndisabled() {
			if (this.oldPwd.length > 5 && this.newPwd.length > 5 && this.newPwdvlt.length > 5) {
				return false;
			}
			return true;
		}
	},
	onLoad(options) {
		_self = this;
		const tmpcnt = options.cnt;
		if (tmpcnt) {
			_self.oldwechat = _self.wechat = decodeURIComponent(tmpcnt);
		}
	},
	methods: {
		/* 取消设置 */
		back() {
			uni.navigateBack({ delta: 1 });
		},

		/* 设置用户名 */
		save() {
			if (_self.$util.checkPassword(_self.oldPwd, _self.oldPwd)) {
				//验证旧密码
				if (_self.$util.checkPassword(_self.newPwd, _self.newPwdvlt)) {
					//验证新密码
					_self.loadModal = true;
					const param = {
						originalPassword: _self.oldPwd,
						password: _self.newPwd
					};
					_self.$http.httpRequest('user/changePassword', param, true).then(res => {
						_self.loadModal = false;
						if (res._tranStatus) {
							_self.back();
						} else {
							_self.oldPwd = '';
							_self.newPwd = '';
							_self.newPwdvlt = '';
							uni.showToast({
								icon: 'none',
								title: res._exceptionLocalizedMessage
							});
						}
					});
				}
			}
		}
	}
};
</script>
